package com.zdp.distributedemo.service;

import com.zdp.distributedemo.lock.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author sesshomaru
 * @date 2020/10/11 0:54
 */
@Service
@Slf4j
public class SchedulerRedissonService {

    @Autowired
    private RedissonClient redissonClient;

    @Scheduled(cron = "0/1 * * * * ?")
    public void sendSms(){
        // 获取锁
        RLock lock = redissonClient.getLock("sendSms");
        // 设置初始的过期时间为30s
        try {
            lock.lock(30, TimeUnit.SECONDS);
            log.info("进入锁~");
            Thread.sleep(10000);
            log.info("向158xxxxxx发送短信! ");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            log.info("解锁~");
        }

    }
}
